package hos.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: HttpRequestSoap </p>
 * <p>Description: WebService 请求体 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/3/3 19:14
 */
public class HttpRequestSoap extends HttpRequest<HttpRequestSoap> implements HttpBaseRequestSoap<HttpRequestSoap> {

    private String nameSpace;

    private boolean dotNet = true;

    private int version;

    private String methodName;

    private Map<String, String> headerBody = new HashMap<>();

    @Override
    public HttpRequestSoap init() {
        if (headerBody == null) {
            headerBody = new HashMap<>();
        }
        return super.init();
    }

    @Override
    public HttpRequestSoap init(String methodName, String nameSpace) {
        setMethodName(methodName);
        setNameSpace(nameSpace);
        return this;
    }

    @Override
    public String getNameSpace() {
        if (nameSpace == null || nameSpace.length() == 0) {
            return nameSpace = config().getNameSpace();
        }
        return nameSpace;
    }

    @Override
    public String getMethodName() {
        return methodName;
    }

    @Override
    public HttpTag.Soap getSoapTag() {
        return new HttpTag.Soap(getMethodName());
    }

    /**
     * WebService 这个不起作用，已经固定死了
     *
     * @param contentType 请求数据的类型
     */
    @Override
    @Deprecated
    public HttpRequestSoap contentType(String contentType) {
        return super.contentType(contentType);
    }

    @Override
    public String getContentType() {
        if (getVersion() == SoapConst.VER12) {
            return HttpContentType.SOAP_XML_CHARSET_UTF_8;
        }
        return HttpContentType.XML_CHARSET_UTF_8;
    }

    @Override
    public HttpRequestSoap setNameSpace(String nameSpace) {
        if (nameSpace == null) {
            return this;
        }
        this.nameSpace = nameSpace;
        return this;
    }

    @Override
    public HttpRequestSoap setMethodName(String methodName) {
        this.methodName = methodName;
        return this;
    }

    @Override
    public boolean getDotNet() {
        return dotNet;
    }

    @Override
    public HttpRequestSoap setDotNet(boolean dotNat) {
        this.dotNet = dotNat;
        return this;
    }

    @Override
    public int getVersion() {
        if (version == 0 || version == -1) {
            return version = SoapConst.VER12;
        }
        return version;
    }

    @Override
    public HttpRequestSoap setVersion(int version) {
        if (version != 0 && version != -1){
            this.version = version;
        }
        return this;
    }

    @Override
    public Map<String, String> getHeaderBody() {
        return headerBody;
    }

    @Override
    public HttpRequestSoap headerBody(String key, String value) {
        headerBody.put(key, value);
        return this;
    }

    @Override
    public HttpRequestSoap headerBody(Map<String, String> value) {
        if (value != null) {
            headerBody.putAll(value);
        }
        return this;
    }

    @Override
    public String getSoapAction() {
        return getNameSpace() + getMethodName();
    }

    @Override
    public Map<String, String> getHeader() {
        Map<String, String> header = super.getHeader();
        header.put("User-Agent", SoapConst.USER_AGENT);
        if (getVersion() != SoapConst.VER12) {
            header.put("SOAPAction", getSoapAction());
        }
        // 直接放到这里不起作用，已经放到 contentType 中了
//        if (getVersion() == SoapConst.VER12) {
//            header.put("Content-Type", HttpContentType.SOAP_XML_CHARSET_UTF_8);
//        } else {
//            header.put("Content-Type", HttpContentType.XML_CHARSET_UTF_8);
//        }
        // Okhttp 会自动处理，这里不需要设置，如果设置的话，就不会自动处理了
//        header("Accept-Encoding", "gzip");
        return header;
    }

    @Override
    public List<HttpParam> getData() {
        List<HttpParam> body = new ArrayList<>();
        body.addAll(getQueryParameters());
        body.addAll(getBodyParameters());
        return body;
    }

    @Override
    public String getDataXml() {
        return HttpSoapUtils.getDataXml(this);
    }

    @Override
    protected void check() {
        if (headerBody == null) {
            headerBody = new HashMap<>();
        }
        super.check();
    }

    @Override
    public void clear() {
        headerBody.clear();
        dotNet = true;
        nameSpace = null;
        methodName = null;
        super.clear();
    }
}
